/* Copyright (c) 2016-2020 Enfry Ltd. All Rights Reserved.
 * 
 * This software is the confidential and proprietary information of Enfry. 
 * You shall not disclose such Confidential Information
 * and shall use it only in accordance with the terms of the agreements
 * you entered into with Enfry.
 */
package com.enfry.common.util.encrypt;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import org.apache.commons.codec.binary.Base64;

/**
 * <请简单说明此类用途>
 * @Title com.enfry.common.util.encrypt.MD5Util2
 * @author longguangxing
 * @date 2018年9月10日
 */
public class MD5Util2 {

	/**
	 * 加密的长度 8位
	 */
	public final static int MD5_LENGTH_8=8;
	/**
	 * 加密的长度 16位
	 */
	public final static int MD5_LENGTH_16=16;
	/**
	 * 加密的长度 32位
	 */
	public final static int MD5_LENGTH_32=32;
	
	// 全局数组
    private final static String[] strDigits = { "0", "1", "2", "3", "4", "5",
            "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"};
    
    // 返回形式为数字跟字符串
    private static String byteToArrayString(byte bByte) {
        int iRet = bByte;
        if (iRet < 0) {
            iRet += 256;
        }
        int iD1 = iRet / 16;
        int iD2 = iRet % 16;
        return strDigits[iD1] + strDigits[iD2];
    }

    // 返回形式只为数字
    private static String byteToNum(byte bByte) {
        int iRet = bByte;
        if (iRet < 0) {
            iRet += 256;
        }
        return String.valueOf(iRet);
    }

    /**
     * 转换字节数组为16进制字串
    @param bByte 待转换的字节数组
    @param judgeMD 是否要转为32位，true=转为32位；false=转为16位
    @return
    *
     */
    private static String byteToString(byte[] bByte,int strlenth,boolean isReturnNum) {
        StringBuffer sBuffer = new StringBuffer();
        for (int i = 0; i < bByte.length; i++) {
        	if(isReturnNum){
        		sBuffer.append(byteToNum(bByte[i]));
        	}else{
        		sBuffer.append(byteToArrayString(bByte[i]));
        	}
        }
        
        if(strlenth==MD5_LENGTH_8){
        	return sBuffer.toString().substring(8,16);
        }else if(strlenth==MD5_LENGTH_16){
        	return sBuffer.toString().substring(8,24);
        }else if(strlenth==MD5_LENGTH_32){
        	return sBuffer.toString();
        }
        return sBuffer.toString();
    }

    /**
     * 对字符串进行MD5加密
    @param strObj 待加密的字符串
    @param judgeMD 是否采用32位，true=转为32位；false=转为16位
    @return
    *
     */
    public static String getMD5Code(String strObj,int strlenth,boolean isReturnNum) {
        String resultString = null;
        try {
            resultString = new String(strObj);
            MessageDigest md = MessageDigest.getInstance("MD5");
            resultString = byteToString(md.digest(strObj.getBytes()),strlenth,isReturnNum);
        } catch (NoSuchAlgorithmException ex) {
            ex.printStackTrace();
        }
        return resultString;
    }
    
    /**
	 * 加密解密算法 执行一次加密，两次解密
	 */ 
	public static String convertMD5(String inStr){
		char[] a = inStr.toCharArray();
		for (int i = 0; i < a.length; i++){
			a[i] = (char) (a[i] ^ 't');
		}
		String s = new String(a);
		return s;
	}
	
	public static String encodeToString(String inStr) throws Exception{
		Base64 base64 = new Base64();
		byte[] textByte = inStr.getBytes("UTF-8");
		//编码
		String encodedText = base64.encodeToString(textByte);
		return encodedText;
	}
	
	public static String decodeToString(String inStr) throws Exception{
		Base64 base64 = new Base64();
		return new String(base64.decode(inStr), "UTF-8");
	}
	
	public static void main(String[] args) throws Exception {
	//	String s = new String("{'msgType':'N','rspCode':'000000','rspData':{'objectData':{},'voucherData':[{'isMain':'0','voucherData':{'accountingEntity':'61','accountingPeriod':'2018-01','area':'MAIN','attrFieldName':'申请人公司','attrFieldNameVariable':'requserOrgId','barCode':'20180111','baseDataTableType':'001','baseDataType':'170111162049852533','changeStatus':'000','createDate':'2018-01-31 15:12:20','dataMode':1,'dateShowFormat':'','echoShow':'','errorMsg':'','fieldKey':'','fieldName':'账套名称','fieldNameVariable':'accountingEntity','fieldType':'001','fieldValue':'','id':'239649702198378496','isMain':'0','modifyDate':'2018-03-12 10:05:56','name':'账套名称','objectTemplateId':'','orgId':'239649702198378496','produceDate':'2018-01-31 15:12:20','refId':'237842671909994496','refType':'000','requiredFlag':'','selectShow':'','showHide':1,'sourceVoucherNo':'','status':'001','sts':'000','type':1,'version':'2','voucherNo':'0005','voucherType':'000'},'voucherHeader':[{'fieldAttr':{'area':'MAIN','baseDataTableType':'001','baseDataType':'170111162049852533','dataMode':'1','dateShowFormat':'','echoShow':[],'fieldName':'申请人公司','fieldNameVariable':'requserOrgId','name':'账套名称','requiredFlag':'','selectShow':[],'showHide':'1','type':'1'},'fieldName':'账套名称','fieldNameVariable':'accountingEntity'},{'fieldAttr':{'area':'','baseDataTableType':'','baseDataType':'','dataMode':'2','dateShowFormat':'','echoShow':[],'fieldName':'','fieldNameVariable':'','name':'凭证序号','requiredFlag':'000','selectShow':[],'showHide':'1','type':'1'},'fieldName':'凭证序号','fieldNameVariable':'voucherNo'},{'fieldAttr':{'area':'','baseDataTableType':'','baseDataType':'','dataMode':'1','dateShowFormat':'','echoShow':[],'fieldName':'单据编号','fieldNameVariable':'barCode','name':'关联数据编号','requiredFlag':'000','selectShow':[],'showHide':'1','type':'1'},'fieldName':'关联数据编号','fieldNameVariable':'refId'},{'fieldAttr':{'area':'','baseDataTableType':'','baseDataType':'','dataMode':'2','dateShowFormat':'','echoShow':[],'fieldName':'','fieldNameVariable':'','name':'生成时间','requiredFlag':'000','selectShow':[],'showHide':'1','type':'7'},'fieldName':'生成时间','fieldNameVariable':'produceDate'},{'fieldAttr':{'area':'MAIN','baseDataTableType':'','baseDataType':'','dataMode':'1','dateShowFormat':'1','echoShow':[],'fieldName':'审批结束时间','fieldNameVariable':'finishTime','name':'会计期间','requiredFlag':'000','selectShow':[],'showHide':'1','type':'7'},'fieldName':'会计期间','fieldNameVariable':'accountingPeriod'}],'voucherItemDatas':[{'branchId':'239649218864611329','branchRefId':'239649218864611331','errorMsg':'','fields':[{'fieldAttr':{'area':'MAIN','baseDataTableType':'','baseDataType':'','dataMode':'1','dateShowFormat':'','echoShow':[],'fieldName':'单据条码','fieldNameVariable':'barCode','name':'text','requiredFlag':'001','selectShow':[],'showHide':'','type':'1'},'fieldNameVariable':'item2','type':'1','value':'20180111'},{'fieldAttr':{'area':'ONE','baseDataTableType':'005','baseDataType':'170111162049857124','dataMode':'4','dateShowFormat':'','echoShow':[],'fieldName':'预算归属部门','fieldNameVariable':'budgetDept','name':'科目','requiredFlag':'001','selectShow':[],'showHide':'1','type':'5'},'fieldNameVariable':'subject','type':'5','value':{'id':'170111162049857124_207475317298692096,170111162049857124_204844218468794368,170111162049857124_204844205524123648','name':'差补,业务招待费,办公用品费'}},{'fieldAttr':{'area':'ONE','baseDataTableType':'','baseDataType':'','dataMode':'1','dateShowFormat':'','echoShow':[],'fieldName':'金额111','fieldNameVariable':'originalAmount','name':'借方金额','requiredFlag':'001','selectShow':[],'showHide':'1','type':'4'},'fieldNameVariable':'amount0','type':'4','value':'888.00'},{'fieldAttr':{'area':'ONE','baseDataTableType':'','baseDataType':'','dataMode':'1','dateShowFormat':'','echoShow':[],'fieldName':'金额111','fieldNameVariable':'originalAmount','name':'贷方金额','requiredFlag':'001','selectShow':[],'showHide':'1','type':'4'},'fieldNameVariable':'amount1','type':'4','value':''}],'id':'243254304050249728','loan':'0','version':'2'},{'branchId':'239649218864611329','branchRefId':'239649218879291393','errorMsg':'','fields':[{'fieldAttr':{'area':'MAIN','baseDataTableType':'','baseDataType':'','dataMode':'2','dateShowFormat':'','echoShow':[],'fieldName':'','fieldNameVariable':'','name':'text','requiredFlag':'001','selectShow':[],'showHide':'','type':'1'},'fieldNameVariable':'item2','type':'1','value':'204844218468794368'},{'fieldAttr':{'area':'ONE','baseDataTableType':'005','baseDataType':'170111162049857124','dataMode':'3','dateShowFormat':'','echoShow':[],'fieldName':'科目','fieldNameVariable':'subject','name':'科目','requiredFlag':'001','selectShow':[],'showHide':'1','type':'5'},'fieldNameVariable':'subject','type':'5','value':{'id':'204844218468794368','name':'业务招待费'}},{'fieldAttr':{'area':'ONE','baseDataTableType':'','baseDataType':'','dataMode':'1','dateShowFormat':'','echoShow':[],'fieldName':'金额111','fieldNameVariable':'originalAmount','name':'贷方金额','requiredFlag':'001','selectShow':[],'showHide':'1','type':'4'},'fieldNameVariable':'amount1','type':'4','value':'888.00'},{'fieldAttr':{'area':'ONE','baseDataTableType':'','baseDataType':'','dataMode':'1','dateShowFormat':'','echoShow':[],'fieldName':'金额111','fieldNameVariable':'originalAmount','name':'借方金额','requiredFlag':'001','selectShow':[],'showHide':'1','type':'4'},'fieldNameVariable':'amount0','type':'4','value':''}],'id':'243254304051298304','loan':'1','version':'2'}],'voucherItemHeader':[{'fieldAttr':{'area':'MAIN','baseDataTableType':'','baseDataType':'','dataMode':'1','dateShowFormat':'','echoShow':[],'fieldName':'单据条码','fieldNameVariable':'barCode','name':'text','requiredFlag':'001','selectShow':[],'showHide':'','type':'1'},'fieldName':'单行文本','fieldNameVariable':'item2','type':'1'},{'fieldAttr':{'area':'ONE','baseDataTableType':'001','baseDataType':'170111162049852533','dataMode':'4','dateShowFormat':'','echoShow':[],'fieldName':'预算归属部门','fieldNameVariable':'budgetDept','name':'科目','requiredFlag':'001','selectShow':[],'showHide':'1','type':'5'},'fieldName':'科目','fieldNameVariable':'subject','type':'5'},{'fieldAttr':{'area':'ONE','baseDataTableType':'','baseDataType':'','dataMode':'1','dateShowFormat':'','echoShow':[],'fieldName':'金额111','fieldNameVariable':'originalAmount','name':'借方金额','requiredFlag':'001','selectShow':[],'showHide':'1','type':'4'},'fieldName':'借方金额','fieldNameVariable':'amount0','type':'4'},{'fieldAttr':{'area':'ONE','baseDataTableType':'','baseDataType':'','dataMode':'1','dateShowFormat':'','echoShow':[],'fieldName':'金额111','fieldNameVariable':'originalAmount','name':'贷方金额','requiredFlag':'001','selectShow':[],'showHide':'1','type':'4'},'fieldName':'贷方金额','fieldNameVariable':'amount1','type':'4'}]}]},'rspMsg':'','sourceId':'237842671909994496','tenantId':'','voucherId':'239649702198378496'}");
		String s = new String("工龄时间");
		System.out.println("原始：" + s);
//		System.out.println("32位MD5后：" + getMD5Code(s,MD5_LENGTH_32,false));
//		System.out.println("16位MD5后：" + getMD5Code(s,MD5_LENGTH_16,false));
//		System.out.println("8位MD5后：" + getMD5Code(s,MD5_LENGTH_8,false));
		
//		System.out.println("32位MD5后：" + getMD5Code(s,MD5_LENGTH_32,true));
//		System.out.println("16位MD5后：" + getMD5Code(s,MD5_LENGTH_16,true));
//		System.out.println("8位MD5后：" + getMD5Code(s,MD5_LENGTH_8,true));
		System.out.println("加密的：" + convertMD5(s));
		System.out.println("解密的：" + convertMD5(convertMD5(s)));
		String b = convertMD5(convertMD5(s));
		if(s.equals(b)){
			System.out.println("aaaaaaaaa");
		}
		
		System.out.println(decodeToString(encodeToString("年龄")));
	}
}
